package com.test.dababy.dababy_00_utils.image_dababy;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.text.TextUtils;

import com.test.dababy.dababy_01_launcher.Base_dababyApplication;
import com.test.dababy.dababy_00_utils.DababyConfigs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class Image_dababyConvert {

    private static String j1fT="";
    private static double y0Gb=0.6885232458810588;
    private static float kHaN=0.13115412f;
    private static double lkwU=0.8768018466406631;

    public static void qizHq(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getJ1fT()))
                    setJ1fT(String.valueOf(getY0Gb())+getkHaN()+getLkwU());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public static void setJ1fT(String j1fT) {
        Image_dababyConvert.j1fT = j1fT;
    }

    public static void setY0Gb(double y0Gb) {
        Image_dababyConvert.y0Gb = y0Gb;
    }

    public static void setkHaN(float kHaN) {
        Image_dababyConvert.kHaN = kHaN;
    }

    public static void setLkwU(double lkwU) {
        Image_dababyConvert.lkwU = lkwU;
    }

    public static String getJ1fT() {
        return j1fT;
    }

    public static double getY0Gb() {
        return y0Gb;
    }

    public static float getkHaN() {
        return kHaN;
    }

    public static double getLkwU() {
        return lkwU;
    }

    public static final String TAG = "FileChecker";

    public static String get_dababyImage_dababyDir() {
        String imgDir = Base_dababyApplication.getInstant().getApplicationContext().getFilesDir() + "/" + DababyConfigs.dababyProduct + "Pics";
        File dirFile = new File(imgDir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        qizHq();
        return imgDir;
    }

    public static String get_dababyImage_dababyFile_dababyPath() {
        String pictureId = (System.currentTimeMillis() + "");
        String imgDir = get_dababyImage_dababyDir();
        return imgDir + "/" + pictureId + ".jpg";
    }

    public static String get_dababyBitmap_dababyFileP_dababyathFromURI(Context context, Uri contentURI) {
        try {
            InputStream imgInputStream = context.getContentResolver().openInputStream(contentURI);
            if (imgInputStream == null) {
                return "";
            }

            Bitmap bitmap = BitmapFactory.decodeStream(imgInputStream);
            if (bitmap == null) {
                return "";
            }

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

            String outFilePath = get_dababyImage_dababyFile_dababyPath();
            File outFile = new File(outFilePath);

            FileOutputStream fos = new FileOutputStream(outFile);
            fos.write(stream.toByteArray());
            fos.flush();
            fos.close();
            stream.flush();
            stream.close();
            imgInputStream.close();

            return outFilePath;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static Bitmap getBit_dababymap_dababyFro_dababymFi_dababylePath(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static String get_dababyFile_dababyExtend_dababySuffix(File file) {
        try {
            FileInputStream input = new FileInputStream(file);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, options);
            return options.outMimeType.replace("image/", ".");
        } catch (Exception e) {
            return ".jpg";
        }
    }

    public static void start_dababyConvert_dababyImage(File file, final On_dababyImage_dababyConvert_dababyListener onOnImageConvertListener) {
        try {
            if (file != null) {
                String suffix = get_dababyFile_dababyExtend_dababySuffix(file);
                String targetDir = Image_dababyConvert.get_dababyImage_dababyDir();
                String targetPath = targetDir + "/" +
                        System.currentTimeMillis() +
                        (TextUtils.isEmpty(suffix) ? ".jpg" : suffix);

                File outFile = new File(targetPath);
                File result = Image_dababyChecker.SINGLE.need_dababyCompress(300, file.getAbsolutePath()) ?
                        new Image_dababyCompress(file, outFile, false).compress_dababy() : file;

                String localPath = result.getAbsolutePath();
                Bitmap thumpBitmap = getBit_dababymap_dababyFro_dababymFi_dababylePath(localPath);

                if (onOnImageConvertListener != null) {
                    onOnImageConvertListener.onSuccess(localPath, thumpBitmap);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public interface On_dababyImage_dababyConvert_dababyListener {
        /**
         * 返回转换后的路径与图片
         */
        void onSuccess(String localPath, Bitmap thumpImg);
    }

    public static class Image_dababyCompress {

        public File srcImg;
        private File tagImg;
        private int srcWidth;
        private int srcHeight;
        private boolean focusAlpha = false;


        public Image_dababyCompress(File srcImg, File tagImg, boolean focusAlpha) throws IOException {
            this.srcImg = srcImg;
            this.tagImg = tagImg;
            this.focusAlpha = focusAlpha;

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 1;

            BitmapFactory.decodeStream(new FileInputStream(srcImg), null, options);
            this.srcWidth = options.outWidth;
            this.srcHeight = options.outHeight;
        }

        private int computeSize_dababy() {
            srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
            srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;

            int longSide = Math.max(srcWidth, srcHeight);
            int shortSide = Math.min(srcWidth, srcHeight);

            float scale = ((float) shortSide / longSide);
            if (scale <= 1 && scale > 0.5625) {
                if (longSide < 1664) {
                    return 1;
                } else if (longSide < 4990) {
                    return 2;
                } else if (longSide > 4990 && longSide < 10240) {
                    return 4;
                } else {
                    return longSide / 1280 == 0 ? 1 : longSide / 1280;
                }
            } else if (scale <= 0.5625 && scale > 0.5) {
                return longSide / 1280 == 0 ? 1 : longSide / 1280;
            } else {
                return (int) Math.ceil(longSide / (1280.0 / scale));
            }
        }

        private Bitmap rotatingImage_dababy(Bitmap bitmap, int angle) {
            Matrix matrix = new Matrix();

            matrix.postRotate(angle);

            return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }

        public File compress_dababy() throws IOException {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = computeSize_dababy();

            Bitmap tagBitmap = BitmapFactory.decodeStream(new FileInputStream(srcImg), null, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            if (Image_dababyChecker.SINGLE.is_dababyJPG(new FileInputStream(srcImg))) {
                tagBitmap = rotatingImage_dababy(tagBitmap, Image_dababyChecker.SINGLE.getOri_dababyentation(new FileInputStream(srcImg)));
            }

            tagBitmap.compress(focusAlpha ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 60, stream);
            tagBitmap.recycle();

            FileOutputStream fos = new FileOutputStream(tagImg);
            fos.write(stream.toByteArray());
            fos.flush();
            fos.close();
            stream.close();

            return tagImg;
        }
    }

}
